//
//  TelephoneViewController.m
//  盘盈贷1.2
//
//  Created by iOS on 15/3/6.
//  Copyright (c) 2015年 iOS. All rights reserved.
//

#import "TelephoneViewController.h"
#import "MyselfViewController.h"
#import "APIRequest.h"
#import "AFHTTPRequestOperation.h"
#import "LoginViewController.h"
@interface TelephoneViewController ()
{
    UITextField *name;
    UITextField *phoneNumber;
    UITextField *yzm;
    
}
@end

@implementation TelephoneViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.title = @"手机号绑定";
        self.view.backgroundColor = [UIColor whiteColor];
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGFloat width =  self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    
    name = [[UITextField alloc] initWithFrame:CGRectMake(50, 80, width / 1.4, 40)];
    name.placeholder = @"邀请人手机号(可不填)";
    name.textColor = [UIColor grayColor];
    name.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:name];
    [name release];
    phoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(50, 130, width / 1.4, 40)];
    phoneNumber.placeholder = @"你的手机号";
    phoneNumber.textColor = [UIColor grayColor];
    phoneNumber.borderStyle = UITextBorderStyleRoundedRect;
    phoneNumber.clearButtonMode = UITextFieldViewModeWhileEditing;
    phoneNumber.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:phoneNumber];
    [phoneNumber release];
    yzm = [[UITextField alloc] initWithFrame:CGRectMake(50, 180, width / 2, 40)];
    yzm.placeholder = @"验证码";
    yzm.borderStyle = UITextBorderStyleRoundedRect;
   
    yzm.textColor = [UIColor grayColor];
    yzm.borderStyle = UITextBorderStyleLine;
    [self.view addSubview:yzm];
    [yzm release];
    UIButton *test = [UIButton buttonWithType:UIButtonTypeSystem];
    test.frame = CGRectMake(90, 180, width / 5, 40);
    test.center = CGPointMake(width / 1.35, 200);
    [test setTitle:@"获取验证码" forState:UIControlStateNormal];
    test.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    test.backgroundColor = [UIColor redColor];
    [test setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [test addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:test];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(70, 225, width / 1.1, 40)];
    label.text = @"已阅读并同意  <网站服务协议>";
    [self.view addSubview:label];
    [label release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 290, width / 1.4, 40);
    button.center = CGPointMake(width / 2, 280);
    [button setTitle:@"完成注册" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:14];
    button.backgroundColor = [UIColor redColor];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(finish:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    NSLog(@"***********%@", self.name);
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == phoneNumber) {
        if (textField == phoneNumber && range.location >= 11 && range.length == 0) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"手机号码不正确" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
        }
        return YES;
    }else{
        return YES;
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == phoneNumber) {
        APIRequest *manage = [APIRequest sharedClient];
        manage.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys: phoneNumber.text, @"phone", nil];
        
        [manage POST:@"account/IsExsistphone?" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
            if ([[[responseObject firstObject] objectForKey:@"value"]isEqualToString:@"true"]) {
                
            }else {
                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"手机号码已存在" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [alert show];
        }];
    }
}

- (void)test:(id)sender
{
   
    if(phoneNumber.text.length == 0){
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"请填写手机号码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        
    }
    
//    APIRequest *manage1 = [APIRequest sharedClient];
//     manage1.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
//    
//    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys: phoneNumber.text, @"phone", yzm.text, @"tpl_id",nil];
//    
//    [manage1 POST:@"account/SendRegisterPhone?" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
//     
//       
//         NSLog(@"~~~~~~~~~~~~%@", responseObject);
//        if ([[responseObject firstObject] objectForKey:@"value"]) {
//          NSLog(@"~~~~~~~~~~~~%@", responseObject);
//
//            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"成功获取验证码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
//            [alert show];
//          
//          
//        }
////        NSString *str = [[responseObject firstObject] objectForKey:@"value"];
////        yzm.text = str;
//        
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
////                UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"失败" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
////                [alert show];
//      NSLog(@"error : %@", error);
//    }];
    NSString *str = @"http://www.cloudpeng.com:8102/account/SendRegisterPhone?phone=18516276195&tpl_id=420375";
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *ss = [[responseObject firstObject] objectForKey:@"value"];
        yzm.text = ss;
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"~~~~~~~%@",error);
    }];
    
    
}
- (void)finish:(id)sender
{
 
    NSString *s1 = self.name;
    NSString *s2 = self.pwd;
    NSString *s3 = phoneNumber.text;
    
    NSString *str = [NSString stringWithFormat:@"http://www.cloudpeng.com:8102/account/Register?username=%@&pwd=%@&phone=%@", s1,s2,s3];
    
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"~~~~~~%@", responseObject);
        if ([[[responseObject firstObject] objectForKey:@"value"] isEqualToString:@"true"]) {
            LoginViewController *login = [[LoginViewController alloc] init];
            [self.navigationController pushViewController:login animated:YES];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"~~~~~~~%@",error);
    }];
    NSLog(@"+++++++%@", self.name);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
